package com.fingard.dsp.bank.directbank.jsjnrcb01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class JSJNRCB01Balance extends JSJNRCB01Base implements IBalance{
    //单个余额查询和批量余额查询，统一采用流处理方式
    //1、循环调用Req7506BalQry.readRecord()读取一个请求明细，然后写入发银行的XML对象
    //2、发银行
    //3、循环调用XmlTextReader.read()读取一个银行返回节点，赋值返回ATS的明细对象，
    //   然后调用RetAts7506BalQry.writeRetDetail()，写入返回ATS的明细
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {//余额查询
        //余额查询返回ATS的对象
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
        	JSJNRCB01Header tmpBnkHeader = new JSJNRCB01Header();
        	String OpName = "B2EActBalQry";
            tmpBnkHeader.TransCode = OpName;
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
			writeCommenHeader(xmlWriter,tmpBnkHeader);
			int qrySize = 0;
			xmlWriter.writeStartElement("Body");
			xmlWriter.writeStartElement("List");
            while (p_req7506.readRecord()) {
            	qrySize++;
                xmlWriter.writeStartElement("Map");
                xmlWriter.writeElementString("AcNo", p_req7506.readedDetail.actNum);
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                //每次查询条数不能超过10条
                if(qrySize%10==0 && qrySize>0) {
                	String[] tmpRetBkStr = sendToBank(xmlWriter.toXmlString(),OpName);
                    if (tmpRetBkStr[0].length() == 0) {
                        StringBuilder tmpSbMsg = new StringBuilder();
                        XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetBkStr[1], tmpSbMsg);
                        if (tmpBkReader != null) {
                        	String returnCode = tmpBkReader.getFirstText("/transaction/Head/IBSReturnCode");
        					String returnMsg = tmpBkReader.getFirstText("/transaction/Head/IBSReturnMsg");
                            if (returnCode != null && returnCode.equals("000000")) {
                            	while(tmpBkReader.read("/transaction/Body/List/Map")){
                                    RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();

                                    tmpRetDetail.respCode = "0";

                                    tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleText("Balance");
                                    tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleText("AvailBal");
                                    //银行未返回 余额-可用
                                   // tmpRetDetail.frozenBal = tmpBkReader.readedXElement.getSingleText("freezeBalance");

                                    tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("AcNo");

                                    retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                                }
                            } else {
                                retAts.writeRespCode("-1");
                                retAts.writeRespInfo(returnMsg);
                                break ;
                            }
                        } else {
                            retAts.writeRespCode("-1");
                            retAts.writeRespInfo(tmpSbMsg.toString());
                            break ;
                        }
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmpRetBkStr[1]);
                        break ;
                    }
                }
            }           
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
    	
    	return null ;
    	
    }

}
